package com.education.welco.controller;

import com.education.common.pojo.AjaxResult;
import com.education.welco.pojo.VirtualReality;
import com.education.welco.pojo.vo.VirtualRealityVO;
import com.education.welco.service.IVirtualRealityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 虚拟仿真 controller业务
 *
 * @Author: liftsail
 * @Date: 2023-04-13
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/virtualReality")
public class VirtualRealityController {

    @Autowired
    private IVirtualRealityService virtualRealityService;

    /**
     * 列表查询虚拟仿真
     */
    @GetMapping("/list")
    public AjaxResult list(VirtualReality virtualReality, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<VirtualReality> list = virtualRealityService.selectVirtualRealityList(virtualReality);
        PageInfo pageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功！", pageInfo);
    }


    /**
     * 新增保存虚拟仿真
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody VirtualReality virtualReality) {
        virtualRealityService.insertVirtualReality(virtualReality);
        return AjaxResult.success("添加成功！");
    }


    /**
     * 主键详情虚拟仿真
     */
    @GetMapping("/selectOne")
    public AjaxResult selectOne(Integer id) {
        VirtualReality result = virtualRealityService.selectVirtualRealityById(id);
        return AjaxResult.success("查询成功！", result);
    }

    /**
     * 修改保存虚拟仿真
     */
    @PostMapping("/update")
    public AjaxResult editSave(@RequestBody VirtualReality virtualReality) {
        if ("1".equals(virtualReality.getIsDel()) || "2".equals(virtualReality.getStatus())) {
            //查询资源是否被分享到学校
            Integer count = virtualRealityService.selectVirtualRealitySchool(virtualReality.getId());
            if (count > 0) {
                return AjaxResult.error("资源被分享勿删除或停用!");
            }
        }
        virtualRealityService.updateVirtualReality(virtualReality);
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删除虚拟仿真
     */
    @PostMapping("/del")
    public AjaxResult remove(String ids) {
        virtualRealityService.deleteVirtualRealityByIds(ids);
        return AjaxResult.success("删除成功！");
    }

    /**
     * 学校分配资源列表查询附带回显
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/schoolVirtualRealityList")
    public AjaxResult schoolVirtualRealityList(@RequestParam Integer schoolId) {
        List<VirtualReality> virtualRealityList = virtualRealityService.schoolVirtualRealityList(schoolId);
        return AjaxResult.success(virtualRealityList);
    }

    /**
     * 批量编辑学校虚拟仿真资源
     *
     * @param vrIds    虚拟仿真资源id集合
     * @param schoolId 学校id
     * @return
     */
    @GetMapping("/updateSchoolShareVR")
    public AjaxResult updateSchoolShareVR(@RequestParam String vrIds, @RequestParam Integer schoolId) {
        Integer num = virtualRealityService.updateSchoolShareVR(vrIds, schoolId);
        return AjaxResult.success("编辑成功");
    }

    //学校有资源->老师将资源添加到课程->学生根据课堂查看资源->管理员将学校资源取消后课程关联表资源存在不能观看(资源下架)

    /**
     * 学校老师查询学校目前有权限的虚拟仿真资源  新增时用于列表查询
     *
     * @param schoolId
     * @return
     */
    @GetMapping("/schoolShareVRList")
    public AjaxResult selectSchoolShareVRList(@RequestParam Integer schoolId) {
        List<VirtualReality> virtualRealityList = virtualRealityService.selectSchoolShareVRList(schoolId);
        return AjaxResult.success(virtualRealityList);
    }

    /**
     * 教师端查看为课程分配的全部虚拟资源(资源状态有效|无效)
     *
     * @param schoolId  学校id
     * @param catalogId 课程二级目录id
     * @return
     */
    @GetMapping("/catalogShareVRList")
    public AjaxResult selectCatalogShareVRList(@RequestParam Integer schoolId,
                                               //@RequestParam Integer teaUserId,
                                               @RequestParam Integer catalogId) {
        List<VirtualRealityVO> virtualRealityList = virtualRealityService.selectCatalogShareVRList(schoolId, catalogId);
        return AjaxResult.success(virtualRealityList);
    }

    /**
     * 教师批量编辑课程虚拟仿真资源
     *
     * @param vrIds     虚拟仿真资源id集合
     * @param teaUserId 教师用户id
     * @param catalogId 课程二级目录id
     * @return
     */
    @GetMapping("/updateCatalogShareVR")
    public AjaxResult updateCatalogShareVR(@RequestParam String vrIds,
                                           @RequestParam Integer teaUserId,
                                           @RequestParam Integer catalogId) {
        Integer num = virtualRealityService.updateCatalogShareVR(vrIds, teaUserId, catalogId);
        return AjaxResult.success("编辑成功");
    }

}